//
//  WHHeaderButtonView.m
//  WHPersonalData
//
//  Created by hyf on 2023/7/20.
//  Copyright © 2023 mac. All rights reserved.
//

static NSString *WHHeaderButtonCellID = @"headerButtonCellID";
static NSString *WHLeftImgButtonCellID = @"leftImgButtonCellID";
static NSString *WHBorderButtonCellID = @"buttonBorderCellID";
static NSString *WHBackgroundButtonCellID = @"backgroundButtonCellID";

#import "WHHeaderButtonView.h"
#import "WHHeaderButtonViewCell.h"
#import "WHLeftImgButtonViewCell.h"
#import "WHButtonBorderCell.h"
#import "WHButtonBackgroundCell.h"
#import "WHHeaderButtonModel.h"

@interface WHHeaderButtonView ()<UICollectionViewDelegate, UICollectionViewDataSource>

/// 列表
@property(nonatomic,strong) UICollectionView *mHeaderCollView;

/// 列表布局
@property(nonatomic,strong) UICollectionViewFlowLayout *mLayout;

/// 按钮类型
@property(nonatomic,assign) WHButtonType type;

@end
@implementation WHHeaderButtonView

- (instancetype)initWithFrame:(CGRect)frame type:(WHButtonType)type{
    self = [super initWithFrame:frame];
    if (self) {
        self.type = type;
    }
    return self;
}

-(void)createUIControls
{
    [super createUIControls];
    self.backgroundColor = [UIColor whiteColor];
}
-(void)setUpUIControlsLayout
{
    [super setUpUIControlsLayout];
}

#pragma mark 设置数据
-(void)setModels:(NSMutableArray *)models
{
    _models = models;
    [self addSubview:self.mHeaderCollView];
    [self.mHeaderCollView reloadData];
}

#pragma mark 列表View
- (UICollectionView *)mHeaderCollView{
    if (!_mHeaderCollView)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        BOOL scrollEnabled = NO;
        if(self.models.count>5)
        {
            layout.itemSize = CGSizeMake((ScreenW-20-(5*4))/5,self.frame.size.height);
            scrollEnabled   = YES;
        }else{
            layout.itemSize = CGSizeMake((ScreenW-20-(5*(self.models.count-1)))/self.models.count,self.frame.size.height);
        }
        if(self.type == WHButtonleftImg){
            scrollEnabled   = YES;
        }
        
        // line   跟滚动方向相同的间距
        layout.minimumLineSpacing = 5;
        // item  跟滚动方向垂直的间距
        layout.minimumInteritemSpacing = 5;
        //滚动方向
        layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
        self.mLayout = layout;
        
        _mHeaderCollView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
        _mHeaderCollView.backgroundColor = [UIColor clearColor];
        _mHeaderCollView.delegate   = self;
        _mHeaderCollView.dataSource = self;
        _mHeaderCollView.scrollEnabled = scrollEnabled;
        //内边距
        _mHeaderCollView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _mHeaderCollView.showsHorizontalScrollIndicator = NO;
        _mHeaderCollView.alwaysBounceHorizontal = YES;
        [_mHeaderCollView registerClass:[WHHeaderButtonViewCell class] forCellWithReuseIdentifier:WHHeaderButtonCellID];
        [_mHeaderCollView registerClass:[WHLeftImgButtonViewCell class] forCellWithReuseIdentifier:WHLeftImgButtonCellID];
        [_mHeaderCollView registerClass:[WHButtonBorderCell class] forCellWithReuseIdentifier:WHBorderButtonCellID];
        [_mHeaderCollView registerClass:[WHButtonBackgroundCell class] forCellWithReuseIdentifier:WHBackgroundButtonCellID];
    }
    return  _mHeaderCollView;
}

#pragma mark - <UICollectionViewDelegate, UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.models.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WHHeaderButtonModel *itemModel = self.models[indexPath.row];
    if(self.type == WHButtonleftImg){
        WHLeftImgButtonViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:WHLeftImgButtonCellID forIndexPath:indexPath];
        cell.cateModel = itemModel;
        return cell;
    }else if(self.type == WHButtonBorder){
        WHButtonBorderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:WHBorderButtonCellID forIndexPath:indexPath];
        cell.cateModel = itemModel;
        return cell;
    }else if(self.type == WHButtonTopImg){
        WHHeaderButtonViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:WHHeaderButtonCellID forIndexPath:indexPath];
        cell.cateModel = itemModel;
        return cell;
    }else if(self.type == WHButtonBackground){
        WHButtonBackgroundCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:WHBackgroundButtonCellID forIndexPath:indexPath];
        cell.cateModel = itemModel;
        return cell;
    }
    return [UICollectionViewCell new];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WHHeaderButtonModel *item = self.models[indexPath.item];
    for (int i = 0; i<self.models.count; i++) {
        WHHeaderButtonModel *item = self.models[i];
        if(i == indexPath.row){
            if(item.isSingleCheck){
                if(item.isSel){
                    item.isSel = NO;
                }else{
                    item.isSel = YES;
                }
            }else{
                item.isSel = YES;
            }
        }else{
            item.isSel = NO;
        }
    }
    [self.mHeaderCollView reloadData];
    if (_delegate &&[_delegate respondsToSelector:@selector(WHHeaderButtonViewWithModel:)])
    {
        [_delegate WHHeaderButtonViewWithModel:item];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    WHHeaderButtonModel *item = self.models[indexPath.item];
    NSDictionary *attribute = @{NSFontAttributeName:item.font? item.font:[UIFont systemFontOfSize:14]};
    if(self.type == WHButtonleftImg || self.type == WHButtonBorder || self.type == WHButtonBackground){
        CGSize currentLabelSize = [item.title sizeWithAttributes:attribute];
        //图片前面的边距+图片宽度+图片和标题的间距+cell的间距+整个collectionView左边的间距+整个collectionView右边的间距
        CGFloat itemWidth = ceil(currentLabelSize.width) + 10 + 18 + 5 + 5 + 10 + 10;
        if(!item.noSelImg){
            //cell的间距+整个collectionView左边的间距+整个collectionView右边的间距
            itemWidth = ceil(currentLabelSize.width) + 5 + 10 + 10;
        }
        // item内容超过collectionView宽度的时候，做布局处理
        if (itemWidth + self.mLayout.minimumLineSpacing > self.frame.size.width) {
            itemWidth = self.frame.size.width - self.mLayout.minimumLineSpacing;
        }
        return CGSizeMake(itemWidth, self.frame.size.height);
    }else{
        int w = 0;
        if(self.models.count>5)
        {
            w = (ScreenW-20-(5*4))/5;
        }else{
            w = (ScreenW-20-(5*(self.models.count-1)))/self.models.count;
        }
        return CGSizeMake(w, self.frame.size.height);
    }
}

@end
